package asso.onetoone;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import util.DBPoolUtils;

public class UserDaoImpl implements UserDao{

	@Override
	public User selectById(int id) {
		try {
			Connection c = DBPoolUtils.getConnection();
			String sql = "select uid,uname,infoid,age,sex from `user` left join userinfo using(uid) where uid = ?";
			ResultSet set = DBPoolUtils.query(c, sql, id);
			if (set.next()) {
				User user = new User();
				user.setuId(set.getInt(1));
				user.setuName(set.getString(2));
				
				UserInfo info = new UserInfo();
				info.setInfoId(set.getInt(3));
				info.setAge(set.getInt(4));
				info.setSex(set.getString(5));
				
				user.setInfo(info);
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
